package asapIs134CK.womi.client;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.FroggitAction;
import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.FroggitGameWigdet;
import asapIs134CK.womi.client.games.logoGame.froggitTheFrog.FroggitWorld;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;


public class LogoTesting implements EntryPoint{
	
	private FroggitWorld testingWorld = new FroggitWorld();
	//private FroggitGame testingGame = new FroggitGame();
	private FlexTable testingTable = new FlexTable();
	private List<Button> allButtons = new ArrayList<Button>(2);
	private final DialogBox responseDialog = new DialogBox();
	private final HTML responseString = new HTML();
	private final HorizontalPanel actionPanel = new HorizontalPanel();
	private final Panel gameUIPanel = new FlowPanel();
	private FroggitGameWigdet testingWidget = new FroggitGameWigdet();
	
	public void onModuleLoad() {
		
		testingTable.setText(0, 0, "test launcher");
		testingTable.setText(0, 1, "test description");
		RootPanel.get("LogoTesting").add(testingTable);
		initDialogBox();
		initActionPanel();
		
		int row = testingTable.insertRow(testingTable.getRowCount());
		addToStringTesting(row);
		row = testingTable.insertRow(testingTable.getRowCount());
		addToHTMLStringTesting(row);
		row = testingTable.insertRow(testingTable.getRowCount());
		addActionTesting(row);
		row = testingTable.insertRow(testingTable.getRowCount());
		addWidgetTesting(row);
		
		HorizontalPanel horizontal = new HorizontalPanel();
		horizontal.add(new Label("foobar"));
		horizontal.add(new Label("foobar1"));
		horizontal.add(new Label("foobar2"));
		horizontal.add(new Label("foobar3"));
		horizontal.add(new Label("foobar4"));
		horizontal.add(new Label("foobar5"));
		horizontal.add(new Label("foobar6"));
		horizontal.add(new Label("foobar7"));
		horizontal.add(new Label("foobar8"));
		horizontal.add(new Label("foobar9"));
		horizontal.add(new Label("foobar10"));
		horizontal.add(new Label("foobar11"));
		horizontal.add(new Label("foobar12"));
		horizontal.add(new Label("foobar13"));
		ScrollPanel testScroll = new ScrollPanel();
		testScroll.add(horizontal);
		testScroll.setSize("350px", "200px");
		RootPanel.get("LogoTesting").add(testScroll);
	}
	
	private void addWidgetTesting(int row) {
		Button WidgetTesting = new Button("launch Froggit the Frog");
		allButtons.add(WidgetTesting);
		testingTable.setWidget(row, 0, WidgetTesting);
		testingTable.setText(row, 1, "start to run the game in the dialogBox");
		
		WidgetTesting.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				responseString.setText("Play with Froggit !");
				disactivateButtons();
				testingWidget.setVisible(true);
				responseDialog.center();
				testingWidget.refresh();
			}
		});
	}

	private void initActionPanel() {
		//gameUIPanel.add(testingGame.getTableRepresentation());
		//first create the different possible action
		final Button Up = new Button("Up");
		final Button Down = new Button("Down");
		final Button Left = new Button("Left");
		final Button Right = new Button("Right");
		//then create the common handler
		class ActionButtonHandler implements ClickHandler{
			private FroggitAction _action;
			public ActionButtonHandler(FroggitAction act){
				_action = act;
			}
			@Override
			public void onClick(ClickEvent event) {
				//testingGame.addNewAction(_action);
			}
		}
		
		Up.addClickHandler(new ActionButtonHandler(FroggitAction.MoveToTop));
		Down.addClickHandler(new ActionButtonHandler(FroggitAction.MoveToDown));
		Left.addClickHandler(new ActionButtonHandler(FroggitAction.MoveToLeft));
		Right.addClickHandler(new ActionButtonHandler(FroggitAction.MoveToRight));
		
		final Button Apply = new Button("Apply");
		Apply.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				//testingGame.applyAction();
				responseString.setText("action applied");
			}
		});
		
		actionPanel.add(Up);actionPanel.add(Down);
		actionPanel.add(Left);actionPanel.add(Right);
		actionPanel.add(Apply);
		
		gameUIPanel.setVisible(false);
		actionPanel.setVisible(false);
	}

	private void addActionTesting(int row) {
		final Button ActionTesting = new Button("launch ActionTesting");
		allButtons.add(ActionTesting);
		testingTable.setWidget(row, 0, ActionTesting);
		testingTable.setText(row, 1, "launch the first testing game");
		
		ActionTesting.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				responseString.setText("picks actions and apply them after");
				disactivateButtons();
				actionPanel.setVisible(true);
				gameUIPanel.setVisible(true);
				responseDialog.center();
			}
		});
	}

	private void initDialogBox() {
		responseDialog.setTitle("test result: ");
		responseDialog.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
		
		closeButton.getElement().setId("closeButton");
		
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogvPanel");
		dialogVPanel.add(new HTML("<b>create a basic word and display it " +
				"in ascii mode</b>"));
		dialogVPanel.add(responseString);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		gameUIPanel.setVisible(false);
		dialogVPanel.add(gameUIPanel);
		actionPanel.setVisible(false);
		dialogVPanel.add(actionPanel);
		testingWidget.setVisible(false);
		dialogVPanel.add(testingWidget);
		
		dialogVPanel.add(closeButton);
		responseDialog.setWidget(dialogVPanel);
		
		closeButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				responseDialog.hide();
				gameUIPanel.setVisible(false);
				actionPanel.setVisible(false);
				testingWidget.setVisible(false);
				activateButtons();
			}
		});
	}

	private void addToStringTesting(int row){
		final Button toStringButton = new Button("toString method test");
		allButtons.add(toStringButton);
		testingTable.setWidget(row, 0, toStringButton);
		testingTable.setText(row, 1, "launch a test that should display the current map");
		toStringButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				responseString.setText(testingWorld.toString());
				disactivateButtons();
				responseDialog.center();
			}
		});
	}
	
	private void addToHTMLStringTesting(int row){
		final Button toStringButton = new Button("toHTMLString method test");
		allButtons.add(toStringButton);
		testingTable.setWidget(row, 0, toStringButton);
		testingTable.setText(row, 1, "launch a test that should display the current map (with HTML breakline)");
		toStringButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				responseString.setHTML(testingWorld.toHTMLString());
				disactivateButtons();
				responseDialog.center();
			}
		});
	}

	protected void disactivateButtons() {
		changeActivationOnButtons(false);
	}

	protected void activateButtons() {
		changeActivationOnButtons(true);
	}
	
	private void changeActivationOnButtons(boolean b) {
		Iterator<Button> ite = allButtons.iterator();
		while(ite.hasNext()){
			Button currentButton = ite.next();
			currentButton.setEnabled(b);
		}
	}
	
		
/**
		
		RootPanel.get("toStringTest").add(toStringTestButton);
		
		final DialogBox dialogBox = new DialogBox();
		final Label responseLabel = new Label();
		initDialogBox(dialogBox, responseLabel);
		
		class MyHandler implements ClickHandler, KeyUpHandler {

			@Override
			public void onClick(ClickEvent event) {
				runToStringTest();
			}

			@Override
			public void onKeyUp(KeyUpEvent event) {
				if (event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
					runToStringTest();
				}
			}
			private void runToStringTest() {
				toStringTestButton.setEnabled(false);
				responseLabel.setText(testingWorld.toString());
				dialogBox.center();
			}
		}
		
		MyHandler handler = new MyHandler();
		toStringTestButton.addClickHandler(handler);
		addMoreTest(dialogBox, responseLabel);
	}

	private void addMoreTest(final DialogBox dialog, final Label resLabel) {
		//get the table created for testing the logo implementation
		Element supposedTable = RootPanel.get("LogoTesting").getElement();
		//use it as a Table
		TableElement realTable = TableElement.as(supposedTable);
		
		//now we can add row containing testing buttons
		TableRowElement newRow = realTable.insertRow(-1);
		TableCellElement newCell = newRow.insertCell(-1);
		final Button toHTMLStringTestingButton = new Button("test the toHTMLString method");
		toHTMLStringTestingButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				toHTMLStringTestingButton.setEnabled(false);
				
				resLabel.setText(testingWorld.toHTMLString());
				dialog.center();
			}
		});
		newCell.appendChild(toHTMLStringTestingButton.getElement());
	}

	/**
	 * take a DialogBox and the important information to display inside an 
	 * prepare the box
	 * @param dialogBox a popup-dialogBox
	 * @param toStringResponseLabel the information to display inside the 
	 * 	dialogBox when needed
	 * /
	private void initDialogBox(final DialogBox dialogBox, final Label toStringResponseLabel) {
		dialogBox.setTitle("test result: ");
		dialogBox.setAnimationEnabled(true);
		final Button closeButton = new Button("Close");
		
		closeButton.getElement().setId("closeButton");
		
		VerticalPanel dialogVPanel = new VerticalPanel();
		dialogVPanel.addStyleName("dialogvPanel");
		dialogVPanel.add(new HTML("<b>create a basic word and display it " +
				"in ascii mode</b>"));
		dialogVPanel.add(toStringResponseLabel);
		dialogVPanel.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
		dialogVPanel.add(closeButton);
		dialogBox.setWidget(dialogVPanel);
		
		closeButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				dialogBox.hide();
				toStringTestButton.setEnabled(true);
				toStringTestButton.setFocus(true);
			}
		});
	}
*/
}